import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        //当前登录用户信息
        user: {
            userId: "",
            imageId: "",
            token: '',
            loginName: ''
        },
        // 存放路由
        dynamicRoutes: [],
        // 存放tag标签
        stateTagsList: [],
        // tagsview标签显示隐藏
        isCollapse: false,
        msgTypes: [{
            id: 0,
            text: 'text'
        }, {
            id: 1,
            text: 'image'
        }, {
            id: 2,
            text: 'video'
        }, {
            id: 3,
            text: 'other'
        }],
    },
    mutations: {
        // 保存当前登录者的信息
        setUser(state, user) {
            if (user != null) {
                state.user.userId = user.userId
                state.user.imageId = user.avatarUrl || user.imageId
                state.user.token = user.token
                state.user.loginName = user.loginName
            } else {
                state.user.userId = ""
                state.user.imageId = ""
                state.user.token = ""
                state.user.loginName = "1"
            }

        },
        DYNAMIC_ROUTES(state, routes) {
            state.dynamicRoutes = routes
        },
        // Mutations
        /**
         * 选择tag标签
         */
        mutationSelectTags(state, data) {
            let result = false
            for (let i = 0; i < state.stateTagsList.length; i++) {
                if (state.stateTagsList[i].url == data.url) {
                    return result = true
                }
            }
            result === false ? state.stateTagsList.push(data) : ''
        },

        /**
         * 关闭tag标签
         */
        mutationCloseTag(state, data) {
            let result = state.stateTagsList.findIndex(item => item.url === data.url)
            state.stateTagsList.splice(result, 1)
        }
    },
    actions: {
        dynamicRoutes({ commit }, routes) {
            commit('DYNAMIC_ROUTES', routes)
        }
    },
    getters: {
        userId(state) {
            return state.user.userId
        },//根据文字标识获取数字标识 
        getMsgType: (state) => (text) => {
            return state.msgTypes.find(msgType => msgType.text === text).id
        },
        //根据数字标识获取标识
        getMsgTypeText: (state) => (num) => {
            return state.msgTypes.find(msgType => msgType.id === num).text
        },
    },
    modules: {
    }
})

export default store
